home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / windows / dv_ocx / dvctrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  11.8 KB  |  457 lines

  1. // dvctrl.cpp : Implementation of the DVocx OLE control class.
  2.  
  3. #include "stdafx.h"
  4. #include "dvocx.h"
  5.  
  6. #include "std.h"
  7. #include "dvtools.h"
  8. #include "dvstd.h"
  9. #include "Tfundecl.h"
  10. #include "dvGR.h"
  11. #include "grfundecl.h"
  12. #include "vuerfundecl.h"
  13. #include "VOFUNDECL.H"
  14. #include "dvctrl.h"
  15.  
  16. #include "DvocxPpg.h"
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24.  
  25. IMPLEMENT_DYNCREATE(DVocx, COleControl)
  26.  
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Message map
  30.  
  31. BEGIN_MESSAGE_MAP(DVocx, COleControl)
  32.     //{{AFX_MSG_MAP(DVocx)
  33.     ON_WM_TIMER()
  34.     ON_WM_DESTROY()
  35.     ON_WM_PAINT()
  36.     //}}AFX_MSG_MAP
  37.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  38.     ON_MESSAGE(VI_EXPOSE, OnExpose)
  39.     ON_MESSAGE(VI_RESIZE, OnResize)
  40. END_MESSAGE_MAP()
  41.  
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // Dispatch map
  45.  
  46. BEGIN_DISPATCH_MAP(DVocx, COleControl)
  47.     //{{AFX_DISPATCH_MAP(DVocx)
  48.     DISP_PROPERTY_NOTIFY(DVocx, "UpdateRate", m_updateRate, OnUpdateRateChanged, VT_I4)
  49.     DISP_PROPERTY_NOTIFY(DVocx, "UseDoubleBuffering", m_fUseDblBuf, OnUseDoubleBufferingChanged, VT_BOOL)
  50.     DISP_PROPERTY_NOTIFY(DVocx, "ViewFile", m_viewFile, OnViewFileChanged, VT_BSTR)
  51.     DISP_PROPERTY_NOTIFY(DVocx, "AutoUpdate", m_autoUpdate, OnAutoUpdateChanged, VT_BOOL)
  52.     DISP_PROPERTY_NOTIFY(DVocx, "Scale", m_scale, OnScaleChanged, VT_R8)
  53.     DISP_FUNCTION(DVocx, "StartUpdating", StartUpdating, VT_EMPTY, VTS_NONE)
  54.     DISP_FUNCTION(DVocx, "StopUpdating", StopUpdating, VT_EMPTY, VTS_NONE)
  55.     DISP_FUNCTION(DVocx, "SetView", SetView, VT_EMPTY, VTS_BSTR)
  56.     DISP_FUNCTION(DVocx, "CloseView", CloseView, VT_EMPTY, VTS_NONE)
  57.     DISP_FUNCTION(DVocx, "UpdateWindow", UpdateWindow, VT_EMPTY, VTS_NONE)
  58.     DISP_FUNCTION(DVocx, "ZoomTo", ZoomTo, VT_EMPTY, VTS_R8)
  59.     //}}AFX_DISPATCH_MAP
  60.     DISP_FUNCTION_ID(DVocx, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  61. END_DISPATCH_MAP()
  62.  
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // Event map
  66.  
  67. BEGIN_EVENT_MAP(DVocx, COleControl)
  68.     //{{AFX_EVENT_MAP(DVocx)
  69.     // NOTE - ClassWizard will add and remove event map entries
  70.     //    DO NOT EDIT what you see in these blocks of generated code !
  71.     //}}AFX_EVENT_MAP
  72. END_EVENT_MAP()
  73.  
  74.  
  75. /////////////////////////////////////////////////////////////////////////////
  76. // Property pages
  77.  
  78. // TODO: Add more property pages as needed.  Remember to increase the count!
  79. BEGIN_PROPPAGEIDS(DVocx, 1)
  80.     PROPPAGEID(CDvocxPropPage::guid)
  81. END_PROPPAGEIDS(DVocx)
  82.  
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // Initialize class factory and guid
  86.  
  87. IMPLEMENT_OLECREATE_EX(DVocx, "DVOCX.DvocxCtrl.1",
  88.     0xf1e0ab13, 0xa1af, 0x11cf, 0xa7, 0x5e, 0x20, 0x4c, 0x4f, 0x4f, 0x50, 0x20)
  89.  
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // Type library ID and version
  93.  
  94. IMPLEMENT_OLETYPELIB(DVocx, _tlid, _wVerMajor, _wVerMinor)
  95.  
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. // Interface IDs
  99.  
  100. const IID BASED_CODE IID_DDvocx =
  101.         { 0xf1e0ab11, 0xa1af, 0x11cf, { 0xa7, 0x5e, 0x20, 0x4c, 0x4f, 0x4f, 0x50, 0x20 } };
  102. const IID BASED_CODE IID_DDvocxEvents =
  103.         { 0xf1e0ab12, 0xa1af, 0x11cf, { 0xa7, 0x5e, 0x20, 0x4c, 0x4f, 0x4f, 0x50, 0x20 } };
  104.  
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // Control type information
  108.  
  109. static const DWORD BASED_CODE _dwDvocxOleMisc =
  110.     OLEMISC_ACTIVATEWHENVISIBLE |
  111.     OLEMISC_SETCLIENTSITEFIRST |
  112.     OLEMISC_INSIDEOUT |
  113.     OLEMISC_CANTLINKINSIDE |
  114.     OLEMISC_RECOMPOSEONRESIZE;
  115.  
  116. IMPLEMENT_OLECTLTYPE(DVocx, IDS_DVOCX, _dwDvocxOleMisc)
  117.  
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // DVocx::DVocxFactory::UpdateRegistry -
  121. // Adds or removes system registry entries for DVocx
  122.  
  123. BOOL DVocx::DVocxFactory::UpdateRegistry(BOOL bRegister)
  124. {
  125.     if (bRegister)
  126.         return AfxOleRegisterControlClass(
  127.             AfxGetInstanceHandle(),
  128.             m_clsid,
  129.             m_lpszProgID,
  130.             IDS_DVOCX,
  131.             IDB_DVOCX,
  132.             FALSE,                      //  Not insertable
  133.             _dwDvocxOleMisc,
  134.             _tlid,
  135.             _wVerMajor,
  136.             _wVerMinor);
  137.     else
  138.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  139. }
  140.  
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // DVocx::DVocx - Constructor
  144.  
  145. DVocx::DVocx()
  146.     : m_updateRate(250), m_screen(NULL), m_view(NULL),
  147.       m_dp(NULL), ok_to_draw(FALSE)
  148. {
  149.     InitializeIIDs(&IID_DDvocx, &IID_DDvocxEvents);
  150.     dv_winproc = NULL;
  151. }
  152.  
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155. // DVocx::~DVocx - Destructor
  156.  
  157. DVocx::~DVocx()
  158. {
  159.     // TODO: Cleanup your control's instance data here.
  160. }
  161.  
  162.  
  163. /////////////////////////////////////////////////////////////////////////////
  164. // DVocx::OnDraw - Drawing function
  165.  
  166. void DVocx::OnDraw(
  167.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  168. {
  169.     // if m_screen is NULL the OCX window has not yet been created.  The
  170.     // class is recieving an OnDraw call from the system because the 
  171.     // container app. wants the OCX to draw itself on teh container app's
  172.     // window.  Unfortunately, DataViews can't do that because DVTOOLS 
  173.     // does not provide an interface to draw on a specifed HDC.  Therefore,
  174.     // we can not draw the view under this condition.
  175.  
  176.     if (m_screen == NULL)
  177.     {
  178.         pdc->TextOut(0,0,"DataViews OCX", 13);
  179.         return;
  180.     }
  181. }
  182.  
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185. // DVocx::DoPropExchange - Persistence support
  186.  
  187. void DVocx::DoPropExchange(CPropExchange* pPX)
  188. {
  189.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  190.     COleControl::DoPropExchange(pPX);
  191.     PX_Long(pPX, _T("UpdateRate"), m_updateRate, 250);
  192.     PX_Bool(pPX, _T("UseDoubleBuffering"), m_fUseDblBuf, TRUE);
  193.     PX_Bool(pPX, _T("AutoUpdate"), m_autoUpdate, FALSE);
  194.     PX_String(pPX, _T("ViewFile"), m_viewFile, "logo.v");
  195.  
  196.     // TODO: Call PX_ functions for each persistent custom property.
  197.  
  198. }
  199.  
  200.  
  201. /////////////////////////////////////////////////////////////////////////////
  202. // DVocx::OnResetState - Reset control to default state
  203.  
  204. void DVocx::OnResetState()
  205. {
  206.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  207.  
  208.     // TODO: Reset any other control state here.
  209. }
  210.  
  211.  
  212. /////////////////////////////////////////////////////////////////////////////
  213. // DVocx::AboutBox - Display an "About" box to the user
  214.  
  215. void DVocx::AboutBox()
  216. {
  217.     CDialog dlgAbout(IDD_ABOUTBOX_DVOCX);
  218.     dlgAbout.DoModal();
  219. }
  220.  
  221.  
  222. /////////////////////////////////////////////////////////////////////////////
  223. // DVocx message handlers
  224.  
  225. void DVocx::StartUpdating() 
  226. {
  227.     // TODO: Add your dispatch handler code here
  228.     if (m_autoUpdate)
  229.         KillTimer((UINT)m_hWnd);
  230.  
  231.     SetTimer((UINT)m_hWnd, m_updateRate, NULL);
  232.     m_autoUpdate = TRUE;
  233. }
  234.  
  235. void DVocx::StopUpdating() 
  236. {
  237.     KillTimer((UINT)m_hWnd);
  238.     m_autoUpdate = FALSE;
  239. }
  240.  
  241. void DVocx::OnUpdateRateChanged() 
  242. {
  243.     if (m_view == NULL || m_screen == NULL)
  244.         return;
  245.  
  246.     if (m_autoUpdate)
  247.     {
  248.         StartUpdating();
  249.     }
  250.         
  251.     SetModifiedFlag();
  252. }
  253.  
  254. void DVocx::OnTimer(UINT nIDEvent) 
  255. {
  256.     // TODO: Add your message handler code here and/or call default
  257.     
  258.     COleControl::OnTimer(nIDEvent);
  259.     UpdateWindow();
  260. }
  261.  
  262. LONG DVocx::OnExpose(UINT wParam, LONG lParam)
  263. {
  264.     MSG         a_msg;          /* used to call GRwe_convert() */
  265.     WINEVENT    win_event;      /* used to call GRwe_convert() */
  266.  
  267.     a_msg.hwnd = m_hWnd;
  268.     a_msg.message = VI_EXPOSE;
  269.     a_msg.wParam = wParam;
  270.     a_msg.lParam = lParam;
  271.           /* here, GRwe_convert() will fill in the region that  */
  272.           /* should be sent with TscRedraw                      */
  273.     GRwe_convert((ADDRESS)&a_msg,&win_event);
  274.     if ( ok_to_draw == TRUE )
  275.         TscRedraw(m_screen,&(win_event.region));
  276.  
  277.     return 0;
  278. }
  279.  
  280. LONG DVocx::OnResize(UINT wParam, LONG lParam)
  281. {
  282.       TscReset(m_screen);
  283.       return 0;
  284. }
  285.  
  286. LRESULT DVocx::WindowProc( UINT msg, WPARAM wParam, LPARAM lParam )
  287. {
  288.     LRESULT r =  COleControl::WindowProc(msg, wParam, lParam);
  289.  
  290.     if ( dv_winproc != NULL )
  291.         return( CallWindowProc(dv_winproc,m_hWnd, msg, wParam, lParam ));
  292.     return r;
  293. }
  294.  
  295.  
  296. void DVocx::OnDestroy() 
  297. {
  298.     CloseView();
  299.     TscCloseCurrentScreen();
  300.     KillTimer((UINT)m_hWnd);    
  301.     COleControl::OnDestroy();
  302. }
  303.  
  304. void DVocx::Init()
  305. {
  306.     /* Initialize DataViews */
  307.     /* Pass the window onto DataViews */
  308.     m_screen = TscOpenSet( "W", NULL,
  309.                          V_WIN32_WINDOW_HANDLE, m_hWnd,
  310.                          V_ACTIVE_CURSOR,
  311.                          V_END_OF_LIST );
  312. if(!m_screen)
  313.  {
  314.    char buf[50];
  315.    int error_code=TscOpenError();
  316.    sprintf(buf,"Product is not validated. Error code %d.",error_code);
  317.    MessageBox(buf,"Validation Error",MB_OK);
  318.    exit(error_code);
  319.   }
  320.  
  321.  
  322.     /* get a pointer to the function that would */
  323.     /* be the window proc for a window created  */
  324.     /* with TscOpenSet                          */
  325.     if (dv_winproc == NULL)
  326.         GRget(V_WIN32_WINDOWPROC,&dv_winproc,V_END_OF_LIST);
  327.  
  328.     GRset(V_WIN32_DOUBLE_BUFFER, m_fUseDblBuf,V_END_OF_LIST);
  329.     if (m_viewFile[0])
  330.     {
  331.         SetView(m_viewFile);
  332.         m_scale = TdpGetScale(m_dp);
  333.         
  334.         if (m_autoUpdate)
  335.             StartUpdating();
  336.     }
  337. }
  338.  
  339. void DVocx::Paint (WPARAM wParam, LPARAM lParam)
  340. {
  341.     PAINTSTRUCT ps;
  342.     MSG         a_msg;          /* used to call GRwe_convert() */
  343.     WINEVENT    win_event;      /* used to call GRwe_convert() */
  344.  
  345.     if ( dv_winproc == NULL )
  346.     {
  347.         BeginPaint(&ps);
  348.         EndPaint(&ps);
  349.         return;
  350.     }
  351.  
  352.     /* this is how you should handle a WM_PAINT message for */
  353.     /* a DVtools window */
  354.     a_msg.hwnd = m_hWnd;
  355.     a_msg.message = WM_PAINT;
  356.     a_msg.wParam = wParam;
  357.     a_msg.lParam = lParam;
  358.     /* GRwe_convert will call BeginPaint() and EndPaint()     */
  359.     /* and fill in the win_event structure with the rectangle */
  360.     /* that needs to be redrawn                               */
  361.     GRwe_convert((ADDRESS)&a_msg,&win_event);
  362.     if ( win_event.type == V_EXPOSE )
  363.       TscRedraw(m_screen,&(win_event.region));
  364. }
  365.  
  366. void DVocx::OnPaint() 
  367. {
  368.     CPaintDC dc(this); // device context for painting
  369.     
  370.     if (m_screen == NULL)
  371.         Init();
  372.  
  373.     Paint((WPARAM)dc.m_hDC,0);
  374. }
  375.  
  376. void DVocx::SetView(LPCTSTR szViewFile) 
  377. {
  378.     if (szViewFile == NULL)
  379.         return;
  380.  
  381.     if (szViewFile[0] == '\0')
  382.         return;
  383.  
  384.     if (m_screen == NULL)
  385.         return;
  386.  
  387.     if (m_view)
  388.         CloseView();
  389.  
  390.     m_view = TviLoad( (LPSTR)szViewFile );
  391.     TviOpenData( m_view );
  392.  
  393.     m_dp = TdpCreate( m_screen, m_view, (RECTANGLE*)NULL, (RECTANGLE*)NULL );
  394.     ok_to_draw = TRUE;
  395.  
  396.     TviReadData( m_view );
  397.     TdpDraw( m_dp );
  398. }
  399.  
  400. void DVocx::CloseView() 
  401. {
  402.     TscSetCurrentScreen(m_screen);
  403.     TdpDestroy(m_dp);
  404.     TviDestroy(m_view);
  405.     m_view = NULL;
  406.     m_dp = NULL;
  407.     ok_to_draw = FALSE;
  408.     // TODO: Add your dispatch handler code here
  409.  
  410. }
  411.  
  412. void DVocx::UpdateWindow() 
  413. {
  414.     TscSetCurrentScreen(m_screen);
  415.     TviReadData( m_view );
  416.     TdpDrawNext( m_dp );
  417. }
  418.  
  419. void DVocx::OnUseDoubleBufferingChanged() 
  420. {
  421.     if (m_screen)
  422.         GRset(V_WIN32_DOUBLE_BUFFER, m_fUseDblBuf,V_END_OF_LIST);
  423. }
  424.  
  425. void DVocx::OnViewFileChanged() 
  426. {
  427.     SetView(m_viewFile);
  428. }
  429.  
  430. void DVocx::OnAutoUpdateChanged() 
  431. {
  432.     if (m_autoUpdate)
  433.         StartUpdating();
  434.     else
  435.         StopUpdating();
  436. }
  437.  
  438. void DVocx::OnScaleChanged() 
  439. {
  440.     if (m_dp)
  441.     {
  442.         double scale = TdpGetScale(m_dp);
  443.  
  444.         TdpZoom(m_dp, m_scale / scale);
  445.         Invalidate();
  446.     }
  447. }
  448.  
  449. void DVocx::ZoomTo(double scale) 
  450. {
  451.     if (m_dp)
  452.     {
  453.         TdpZoom(m_dp, scale);
  454.         Invalidate();
  455.     }
  456. }
  457.